﻿using System.ComponentModel;
using System.Windows;
using OpenAiClient.Models;
using OpenAiClient.Services;
using OpenAiClient.ViewModels;
using Prism.Events;
using Prism.Ioc;
using Prism.Regions;

namespace OpenAiClient.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    // ReSharper disable once RedundantExtendsListEntry
    public partial class MainWindow : Window
    {
        private readonly TitleView _titleView;

        private SearchView? _searchView;
        private UpgradeView? _upgradeView;
        private AboutView? _aboutView;
        private HistoryView? _historyView;
        private SettingView? _settingView;
        private FeedbackView? _feedbackView;
        private FrameworkView? _frameworkView;

        private readonly IContainerExtension _container;
        private readonly IRegionManager _regionManager;
        private IRegion? _region;

        public MainWindow(
            IConfigService configService,
            IContainerExtension container,
            IRegionManager regionManager,
            IEventAggregator eventAggregator
        )
        {
            InitializeComponent();

            _container = container;
            _regionManager = regionManager;

            _titleView = new TitleView(configService);
            _titleView.OnMinimize += () => WindowState = WindowState.Minimized;
            _titleView.OnClose += Close;
            _titleView.OnMenuItemClicked += OnMenuItemClicked;

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                DataContext = new MainWindowViewModel(eventAggregator);
            }
        }

        private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            //注册标题
            var titleRegion = _regionManager.Regions["TitleRegion"];
            titleRegion.Add(_titleView);
            titleRegion.Activate(_titleView);

            //注册View
            {
                //解析View
                _searchView = _container.Resolve<SearchView>();
                _upgradeView = _container.Resolve<UpgradeView>();
                _aboutView = _container.Resolve<AboutView>();
                _historyView = _container.Resolve<HistoryView>();
                _settingView = _container.Resolve<SettingView>();
                _feedbackView = _container.Resolve<FeedbackView>();
                _frameworkView = _container.Resolve<FrameworkView>();

                //添加View
                _region = _regionManager.Regions["ContentRegion"];
                _region.Add(_searchView);
                _region.Add(_upgradeView);
                _region.Add(_aboutView);
                _region.Add(_historyView);
                _region.Add(_settingView);
                _region.Add(_feedbackView);
                _region.Add(_frameworkView);
            }
        }

        private void OnMenuItemClicked(MenuType menuType)
        {
            switch (menuType)
            {
                case MenuType.Search:
                    _region?.Activate(_searchView);
                    break;
                case MenuType.Upgrade:
                    _region?.Activate(_upgradeView);
                    break;
                case MenuType.About:
                    _region?.Activate(_aboutView);
                    break;
                case MenuType.History:
                    _region?.Activate(_historyView);
                    break;
                case MenuType.Setting:
                    _region?.Activate(_settingView);
                    break;
                case MenuType.Feedback:
                    _region?.Activate(_feedbackView);
                    break;
                case MenuType.Framework:
                    _region?.Activate(_frameworkView);
                    break;
            }
        }
    }
}
